package com.minelx.sequence.pattern;

import com.minelx.sequence.core.OrMatcher;
import com.minelx.sequence.core.RepeatMatcher;
import com.minelx.sequence.core.ReturnMatcher;
import com.minelx.sequence.core.StringScanner;

import static com.minelx.sequence.core.ReturnMatcher.always;

public class StarMatcher implements ReturnMatcher<ReturnMatcher<?, StringScanner>, StringScanner> {
	private final ReturnMatcher<?, StringScanner> repeated;

	public StarMatcher(ReturnMatcher<?, StringScanner> repeated) {
		this.repeated = repeated;
	}

	@Override
	public boolean isMatch(StringScanner clue) {
		return clue.exists("*");
	}

	@Override
	public ReturnMatcher<?, StringScanner> onMatched(StringScanner clue) {
		clue.goes("*");
		OrMatcher<Object, StringScanner> result = OrMatcher.empty();
		result.add(new RepeatMatcher<>(repeated));
		result.add(always(""));
		return result;
	}
}
